//
//  Model.m
//  CandyJump
//
//  Created by Bon Lam on 26/02/2011.
//  Copyright 2011 Peru Republic. All rights reserved.
//

#import "Model.h"

#import "CCAction.h"
#import "CCActionInterval.h"
#import "CCAnimation.h"
#import "CCNode.h"
#import "CCSprite.h"



#define MODEL_DEFAULT_ANIMATION		@"animIdle"

//=====================================================================================================================
@implementation Model

@synthesize batchnodeName;
@synthesize defaultFrameName;
@synthesize defaultAnimationName;
@synthesize animationDictionary;
@synthesize renderOffset;
@synthesize sprite;

#pragma mark Model - Object Functions
//---------------------------------------------------------------------------------------------------------------------
- (id)init
{
	self = [super init];
	if (self == nil)  {return self;}
	
	renderOffset = CGPointZero;
	
	xFlip = NO;
	yFlip = NO;
	
	defaultAnimationName = MODEL_DEFAULT_ANIMATION;
	
	return self;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)dealloc
{
	[batchnodeName release]; batchnodeName = nil;
	[defaultFrameName release]; defaultFrameName = nil;
	[defaultAnimationName release]; defaultAnimationName = nil;
	[animationDictionary release]; animationDictionary = nil;
	[sprite release]; sprite = nil;
	
	[super dealloc];
}

//---------------------------------------------------------------------------------------------------------------------
- (id)copyWithZone:(NSZone *)zone
{
	Model *newModel = [[[self class] allocWithZone:zone] init];
	newModel.batchnodeName = self.batchnodeName;
	newModel.animationDictionary = self.animationDictionary;
	newModel.sprite = [CCSprite spriteWithSpriteFrameName:self.defaultFrameName];
	[newModel playAnimation:self.defaultAnimationName];

	newModel->xFlip =xFlip;
	newModel->yFlip = yFlip;
	
	return newModel;
}

//---------------------------------------------------------------------------------------------------------------------
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{
	NSLog(@"[Model setValue:forUndefinedKey:], the key [%@] is not found in class [%@]", key, [self class]);
}

#pragma mark Model - Setters and Getters


#pragma mark Model - Physics
//---------------------------------------------------------------------------------------------------------------------
- (void)setUpPhysics:(cpSpace *)space
{
	
}

#pragma mark Model - Sprite
//---------------------------------------------------------------------------------------------------------------------
- (void)removeSpriteFromParent
{
	CCNode *parent = sprite.parent;
	[parent removeChild:sprite cleanup:YES];
}

//---------------------------------------------------------------------------------------------------------------------
- (void)playAnimation:(NSString*)animationName
{
	CCAnimation *animation = [animationDictionary objectForKey:animationName];
	
	if (animation == nil || [animation isKindOfClass:[CCAnimation class]] == NO) {
		//NSLog(@"[Model playAnimation:] Error - Unable to play animation ""%@""", animationName);
		return;
	}
	
	CCAction *animationAction = [CCRepeatForever actionWithAction: [CCAnimate actionWithAnimation:animation restoreOriginalFrame:NO]];
	[sprite runAction:animationAction];
}

#pragma mark Model - Update
//---------------------------------------------------------------------------------------------------------------------
- (void)update:(ccTime)dt
{
	
}

@end
